package org.alex.samples.web;

import java.util.List;
//import java.util.Locale;
//import org.springframework.web.servlet.LocaleResolver;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;

import org.alex.samples.model.User;
import org.alex.samples.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class UserController {
	
	@Autowired
	private IUserService userService;
	
//	@Autowired
//	private LocaleResolver localeResolver;
	
	@Autowired
	private MessageSource messageSource;
	
	
//	@RequestMapping(value="/changeLocale")
//	public String changeLocale(HttpServletRequest request,HttpServletResponse response,String localeType){
//		if("en_US".equals(localeType)){
//			localeResolver.setLocale(request, response, Locale.US);
//		}else if("zh_CN".equals(localeType)){
//			localeResolver.setLocale(request, response, Locale.CHINA);
//		}
//		return "redirect:/users";
//	}
	
	//查询全部方法
	@RequestMapping("/users")
	public String users(Model model){
		List<User> list=userService.findAllUsers();
		
		model.addAttribute("edit", messageSource.getMessage("page.index.edit", null,LocaleContextHolder.getLocale()));
		model.addAttribute("userList", list);
		return "user/userList";
	}
	
	//新增方法
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String toAdd(Model model){
		model.addAttribute("user", new User());
		return "user/add";
	}
	
	@RequestMapping(value="/add" ,method=RequestMethod.POST)
	public String add(RedirectAttributes redirectAttributes,@Validated User user,BindingResult result){
		if(result.hasErrors()){
			return "user/add";
		}
		try{
			userService.createUser(user);
			redirectAttributes.addFlashAttribute("message", "添加成功。");
			
		}catch(Exception e){
			e.printStackTrace();
			return "error";
			
		}
		return "redirect:/users";
	}
	
	
	//修改方法
	@RequestMapping(value="/edit/{id}",method=RequestMethod.GET)
	public String toEdit(Model model,@PathVariable Integer id){
		User user=userService.getUserById(id);
		model.addAttribute("user",user);
		return "user/edit";
	}
	
	@RequestMapping(value="/edit/{id}",method=RequestMethod.POST)
	public String edit(RedirectAttributes redirectAttributes,User user,@PathVariable Integer id){
		try{
			user.setId(id);
			userService.editUser(user);
			redirectAttributes.addFlashAttribute("message", "修改成功.");
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "redirect:/users";
	}
	
	
	//删除方法（视频的做法）
//	@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)
//	public String delete(RedirectAttributes redirectAttributes,@PathVariable Integer id){
//		try{
//			userService.deleteUser(id);
//			redirectAttributes.addFlashAttribute("message", "删除成功。");
//		}catch(Exception e){
//			redirectAttributes.addFlashAttribute("message", "删除失败");
//			e.printStackTrace();
//		}
//		return "redirect:/users";
//	}
	
	
	
	//测试删除方法  （添加一个dele.jsp,完善删除功能，提示是否删除）
	@RequestMapping(value="/dele/{id}",method=RequestMethod.GET)
	public String toDele(Model model,@PathVariable Integer id){
		User user=userService.getUserById(id);
		model.addAttribute("user",user);
		return "user/dele";
	}
	
	@RequestMapping(value="/dele/{id}",method=RequestMethod.POST)
	public String delete(RedirectAttributes redirectAttributes,@PathVariable Integer id){
		try{
			userService.deleteUser(id);
//			redirectAttributes.addFlashAttribute("message", "删除成功.");
		}catch(Exception e){
			redirectAttributes.addFlashAttribute("message", "删除失败.");
			e.printStackTrace();
			return "error";
		}
		return "redirect:/users";
	}
	

	
}
